package cn.youyeku.zsxh.ui.practice;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.ConverUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.bean.Question;
import cn.youyeku.zsxh.bean.question_data;
import cn.youyeku.zsxh.ui.List.CommonAdapter;
import cn.youyeku.zsxh.ui.List.ViewHolder;
import cn.youyeku.zsxh.ui.activity.ABS.BaseActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


/**
 * 主页面加载视图
 */


public class QuestionPracticeActivity extends BaseActivity implements View.OnClickListener {
    public static SparseBooleanArray check_question = new SparseBooleanArray();
    public static int[] do_questions ;
    public static String question_id;
    public static String question_title;

    //是否提交试卷 判断是否已经检查到了试卷
    public static boolean submit=false;



    //问题合集的大小
    protected static List<Question> questions = new ArrayList<>();

    //问题的大小
    public static int question_size;


    //fgm的合集
    private List<Fragment> fragments = new ArrayList<>();
    private ViewPager viewPager;




    @Override
    public void onBackPressed() {

        submit=false;
        super.onBackPressed();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.act_questions);


        question_data question_data = (question_data) getIntent().getSerializableExtra("data");
        setTitle(question_data.getQuestion_desc());
        question_id = question_data.getQuestion_id();
        question_title=question_data.getQuestion_desc();
        //base64 转json ->List
        Gson gson = new Gson();
        questions = gson.fromJson(ConverUtils.Base64ToString(question_data.getQuestion_data()), new TypeToken<List<Question>>() {
        }.getType());

        question_size = questions.size();
        for (int i = 0; i < question_size; i++) {
            //new Instance 不重复??? 更加问题大小创建
            fragments.add(QuestionFragment.newInstance(i));
        }

        //添加最后一个扫描结果的fgm加进去
        fragments.add(new ScantronItemFragment());




        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //最后一个
                if(position==question_size){
                    ScantronItemFragment fgm = (ScantronItemFragment) fragments.get(position);
                    fgm.update();
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {
//                LogUtils.w("当前onPageScrollStateChanged" + state);
            }
        });

        //每次切换时候数据不会重新加载
        viewPager.setOffscreenPageLimit(questions.size());


        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size() ;
            }
        });


        //注册本地广播
        LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction("youyeku.jumptonext");
        filter.addAction("youyeku.jumptopage");
        filter.addAction("youyeku.updatefgm");
        lbm.registerReceiver(mMessageReceiver, filter);

        do_questions = new int[question_size];

    }

    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("youyeku.jumptonext")) {
                jumpToNext();
            } else if (intent.getAction().equals("youyeku.jumptopage")) {
                int index = intent.getIntExtra("index", 0);
                jumpToPage(index);
            }else if (intent.getAction().equals("youyeku.updatefgm")){
                for (int pos =0;pos<question_size;pos++){
                    QuestionFragment fgm = (QuestionFragment) fragments.get(pos);
                    fgm.update();

                }
            }
        }
    };

    //跳到下一页
    public void jumpToNext() {
        int position = viewPager.getCurrentItem();
        viewPager.setCurrentItem(position + 1);

    }

    //跳到指定页
    public void jumpToPage(int index) {
        viewPager.setCurrentItem(index);
    }

    @Override
    protected void onDestroy() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(
                mMessageReceiver);
        super.onDestroy();
    }



    @Override
    public void onClick(View v) {

    }


}
